<?php
class Region_model extends CI_Model
{
    private $table = 'region';
    
    public function findById($region_id)
    {
        $this->db->where('region_id', $region_id);
        return $this->db->get($this->table);
    }
    
    public function findParams($params=array())
    {
        if (!empty($params['region_id'])) {
            $this->db->where('region_id', $params['region_id']);
        }
        if (!empty($params['region_name'])) {
            $this->db->like('region_name', $params['region_name']);
        }
        if (!empty($params['region_pinyin'])) {
            $this->db->where('region_pinyin', $params['region_pinyin']);
        }
        if (!empty($params['region_type'])) {
            $this->db->where('region_type', $params['region_type']);
        }
        if (!empty($params['limit'])) {
            $this->db->limit($params['limit']);
        }
        return $this->db->get($this->table);
    }

    public function findByType($parent_id, $region_type)
    {
        $this->db->where('parent_id', $parent_id);
        $this->db->where('region_type', $region_type);
        return $this->db->get($this->table);
    }
    
    /**
     * 获取所有国家的省市区
     * @return multitype:unknown
     */
    public function getNewRegion($params=array(), $isArray=false)
    {
        if (!empty($params['region_type'])) { //单独取省市区
            $this->db->where('region_type', $params['region_type']);
        }
        if (!empty($params['parent_id'])) {
            $this->db->where('parent_id <', 36); //只取中国的省市区
        }
        $result = $this->db->get($this->table);
        $rows = array();
        if ($isArray) {
            $rows = array();
            foreach ($result->result_array() as $row) {
                $rows[$row['region_id']] = $row;
            }
            return $rows;
        }
        return $result;
    }
    
    /**
     * 只获取中国的省市区
     * @return multitype:unknown
     */
    public function getChinaRegion($params=array(), $isArray=false)
    {
        if (!empty($params['region_type'])) { //单独取省市区
            $this->db->where('region_type', $params['region_type']);
        }
        $this->db->where('parent_id <', 36); //只取中国的省市区
        $this->db->order_by('region_abbr', 'ASC');
        $result = $this->db->get($this->table);
        if ($isArray) {
            $rows = array();
            foreach ($result->result_array() as $row) {
                $rows[$row['region_id']] = $row;
            }
            return $rows;
        }
        return $result;
    }
    
    public function findParentId($parentId, $isArray=false)
    {
        if ($parentId) {
            $this->db->where('parent_id', $parentId);
        }
        $this->db->order_by('region_id', 'ASC');
        $result = $this->db->get($this->table);
        if ($isArray) {
            $rows = array();
            foreach ($result->result_array() as $row) {
                $rows[$row['region_id']] = $row;
            }
            return $rows;
        }
        return $result;
    }

    /**
     * 查找多条记录
     */
    public function findRegionIds($ids)
    {
        $this->db->select('region_id, parent_id, region_name, region_type');
        $this->db->from($this->table);
        $this->db->where_in('region_id', $ids);
        $this->db->order_by('region_type', 'ASC');
        return $this->db->get();
    }
    
    /**
     * zhang 20160725
     * @param unknown $region_name
     * @return boolean
     */
    public function findByName($region_name)
    {
        $this->db->where('region_name', $region_name);
        $result = $this->db->get($this->table);
        if($result->num_rows() >0){
            return $result->row();
        }else{
            return false;
        }
    }
}